#include "err_show.h"
#include <Windows.h>
#include <tchar.h>
#include <crtdbg.h>
#include <errno.h>
#include "ts.h"
#include "env.h"

void InvalidParameterHandler(PCTSTR exp, PCTSTR func, PCTSTR file, unsigned int line, uintptr_t pRes)
{
	MessageBox(NULL, L"Debug", L"MSG", 0);
}




BOOL QCreateProcess(PROCESS_INFORMATION &pi) {
	STARTUPINFO si = { sizeof(si) };
	
	TCHAR szPath[MAX_PATH];
	_tcscpy_s(szPath, _countof(szPath), TEXT("NOTEPAD"));
	return CreateProcess(NULL, szPath, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi);
}


BOOL QPromotePrivilege() {
	SHELLEXECUTEINFO sei = { sizeof(SHELLEXECUTEINFO) };
	sei.lpVerb = TEXT("runas");
	sei.lpFile = TEXT("cmd.exe");
	sei.nShow = SW_SHOWNORMAL;
	if (!ShellExecuteEx(&sei))
	{
		DWORD dwStatus = GetLastError();
		if (dwStatus == ERROR_CANCELLED)
		{
			MessageBox(NULL, L"User Canceled", L"MSG", 0);
		}
		else if(dwStatus == ERROR_FILE_NOT_FOUND)
		{
			MessageBox(NULL, L"File Not Found", L"MSG", 0);
		}
		return FALSE;
	}
	return TRUE;
}


BOOL QGetProcessElevation(TOKEN_ELEVATION_TYPE* pElevationType, BOOL* pIsAdmin) {
	HANDLE hTOken = NULL;
	DWORD dwSize;

	if (!OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY, &hTOken))
	{
		return FALSE;
	}
	BOOL bResult = FALSE;
	if (GetTokenInformation(hTOken, TokenElevationType, pElevationType, sizeof(TOKEN_ELEVATION_TYPE), &dwSize))
	{
		BYTE adminSID[SECURITY_MAX_SID_SIZE];
		dwSize = sizeof(adminSID);
		CreateWellKnownSid(WinBuiltinAdministratorsSid, NULL, &adminSID, &dwSize);
		if (*pElevationType == TokenElevationTypeLimited)
		{
			HANDLE hUnfilteredToken = NULL;
			GetTokenInformation(hTOken, TokenLinkedToken, (VOID*)&hUnfilteredToken, sizeof(HANDLE), &dwSize);
			if (CheckTokenMembership(hUnfilteredToken, &adminSID, pIsAdmin)) {
				bResult = TRUE;
			}
			CloseHandle(hUnfilteredToken);
		}
		else
		{
			bResult = FALSE;
		}
	}
	CloseHandle(hTOken);
	return bResult;
}



int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrev, LPSTR pszCmdLine, int nCmdShow)
{	



	//PROCESS_INFORMATION pi;
	//BOOL result = QCreateProcess(pi);

	//if (result)
	//{
	//	CloseHandle(pi.hThread);

	//	WaitForSingleObject(pi.hProcess, 3000);

	//	CloseHandle(pi.hProcess);

	//}
	//QPromotePrivilege();

	TOKEN_ELEVATION_TYPE tType;
	BOOL isAdmin;

	BOOL result = QGetProcessElevation(&tType, &isAdmin);
	if (result)
	{
		MessageBox(NULL, L"Admin", L"MSG", 0);
	}
	else
	{
		MessageBox(NULL, L"Standard", L"MSG", 0);
	}
	return 0;


	//wchar_t dir[1024];
	//DWORD len=1024;


	//GetOSVersion();

	//DWORD cch = GetCurrentDirectory(len, dir);
	//MessageBox(NULL, dir, L"MSG", 0);


	//DumpEnvStrings();
	//LocateTerminalService();
	//ErrShow(5);
	//_CrtSetReportMode(_CRT_ASSERT, 0);
	//_set_invalid_parameter_handler(InvalidParameterHandler);



	//SECURITY_ATTRIBUTES sa;
	//sa.nLength = sizeof(SECURITY_ATTRIBUTES);
	//sa.lpSecurityDescriptor = NULL;
	//sa.bInheritHandle = FALSE;

	//HANDLE hMutex = CreateMutex(&sa, FALSE, TEXT("JeffObj"));
	//if (GetLastError() == ERROR_SUCCESS)
	//{
	//	MessageBox(NULL, L"New", L"MSG", 0);
	//}

	//HANDLE hMutex_a = CreateMutex(&sa, FALSE, TEXT("JeffObj"));
	//{
	//	MessageBox(NULL, L"ERROR_ALREADY_EXISTS", L"MSG", 0);
	//}




	//WCHAR szBuffer[4] = L"";
	//wcscpy(szBuffer, L"abc");

	//BOOL isUni = IsTextUnicode(szBuffer, sizeof(szBuffer), NULL);
	//WCHAR szBuffer_1[3] = L"";
	//errno_t result =  _tcscpy_s(szBuffer_1, 3, L"abc");
	//return 1;
	//return 0;
}